/***************************************************************************
*   Copyright (C) 2010-2011 by swkyer <swkyer@gmail.com>                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef __JLINKUSB_H__
#define __JLINKUSB_H__


#ifdef JLINKUSB_EXPORTS
	#ifdef WIN32
		#ifdef __cplusplus
			#define JLINKUSB_API extern "C" __declspec(dllexport)
		#else
			#define JLINKUSB_API __declspec(dllexport)
		#endif
	#else	//Linux
		#ifdef __cplusplus
			#define JLINKUSB_API extern "C"
		#else
			#define JLINKUSB_API
		#endif
	#endif
#else
	#ifdef WIN32
		#ifdef __cplusplus
			#define JLINKUSB_API extern "C" __declspec(dllimport)
		#else
			#define JLINKUSB_API __declspec(dllimport)
		#endif
	#else	//Linux
		#ifdef __cplusplus
			#define JLINKUSB_API extern "C"
		#else
			#define JLINKUSB_API
		#endif
	#endif
#endif


typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned __int64 uint64_t;
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed __int64 int64_t;


#define JLINK_COMM_DEFAULT_TIMEOUT		2000

#define JLINK_IN_BUFFER_SIZE			2048
#define JLINK_OUT_BUFFER_SIZE			(2*2048 + 4)
#define JLINK_EMU_RESULT_BUFFER_SIZE	64

typedef struct __jlink_usb_s
{
	HANDLE m_PipeIn;
	HANDLE m_PipeOut;
	HANDLE m_hReadEvent;
	HANDLE m_Mutex;
	int m_nTimeout;

	int m_nLastRcvLen;

	uint8_t in_buffer[JLINK_IN_BUFFER_SIZE];
	uint8_t out_buffer[JLINK_OUT_BUFFER_SIZE];
	uint8_t emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
} jlink_usb_t;


JLINKUSB_API jlink_usb_t *jlinkusb_open(int device);
JLINKUSB_API int jlinkusb_close(jlink_usb_t *usbdev);
JLINKUSB_API int jlinkusb_lock(jlink_usb_t *usbdev);
JLINKUSB_API int jlinkusb_unlock(jlink_usb_t *usbdev);
JLINKUSB_API int jlinkusb_read(jlink_usb_t *usbdev, uint8_t *buffp, int count);
JLINKUSB_API int jlinkusb_write(jlink_usb_t *usbdev, uint8_t *buffp, int count);
JLINKUSB_API int jlinkusb_access(jlink_usb_t *usbdev, uint8_t *buffw, int wcount, uint8_t *buffr, int rcount);
JLINKUSB_API int jlinkusb_get_timeout(jlink_usb_t *usbdev);
JLINKUSB_API int jlinkusb_set_timeout(jlink_usb_t *usbdev, int tc);


#endif /* end of __JLINKUSB_H__ */
